package com.wxd.framework.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.wxd.framework.common.FrameConstants;
import com.wxd.framework.common.Criteria;
import com.wxd.framework.common.page.Pagination;
import com.wxd.framework.common.web.RequestUtils;
import com.wxd.framework.entity.User;
import com.wxd.framework.service.UserService;
import com.wxd.framework.vo.TreeItem;

@Controller
@RequestMapping("/")
public class UserController extends BaseController {

	@Autowired
	private UserService userService;
	
	/**
	 * TO:用户管理页面
	 * @return
	 */
	@RequestMapping("/toUserManager.do")
	public String toUserManager(ModelMap model){
		model.addAttribute(FrameConstants.ADMIN_USER_KEY, FrameConstants.ADMIN_USER);
		return "frame/systemManager/userManager";
	}
	
	/**
	 * 停止/恢复用户
	 * @param request
	 * @param response
	 */
	@RequestMapping("/stopOneUser.do")
	public void stopOneUser(HttpServletRequest request, HttpServletResponse response){
		String currUser = super.getCurrUser(request).getUserName();
		String id = RequestUtils.getQueryParam(request, "id");
		String isStopedStr = RequestUtils.getQueryParam(request, "isStoped");
		boolean isStoped = Boolean.parseBoolean(isStopedStr);
		userService.stopOneUser(id, isStoped, currUser);
		super.renderJsonExcuteSuccess(response);
	}
	
	/**
	 * 根据用户获取角色列表
	 * @param request
	 * @param response
	 */
	@RequestMapping("/getRoleTreeByUserId.do")
	public void getRoleTreeByUserId(HttpServletRequest request, HttpServletResponse response){
		String userId = RequestUtils.getQueryParam(request, "userId");
		List<TreeItem> list = userService.getRoleTreeByUserId(userId);
		super.renderList(list, response);
	}
	
	/**
	 * 保存用户的角色列表
	 * @param request
	 * @param response
	 */
	@RequestMapping("/saveUserRoleForUser.do")
	public void saveUserRoleForUser(HttpServletRequest request, HttpServletResponse response){
		String userId = RequestUtils.getQueryParam(request, "userId");
		String roleIds = RequestUtils.getQueryParam(request, "roleIds");
		try{
			userService.saveUserRoleForUser(userId, roleIds);
			super.renderJsonExcuteSuccess(response);
		}catch(Exception ex){
			super.renderException(ex, response);
		}
	}
		
	/**
	 * 新增用户
	 * @param request
	 * @param response
	 */
	@RequestMapping("/addOneUser.do")
	public void addOneUser(HttpServletRequest request, HttpServletResponse response){
		String currUser = super.getCurrUser(request).getUserName();
		String userName = RequestUtils.getQueryParam(request, "userName");
		String realName = RequestUtils.getQueryParam(request, "realName");
		String email = RequestUtils.getQueryParam(request, "email");
		String phoneNumber = RequestUtils.getQueryParam(request, "phoneNumber");
		User user = new User(userName, email, realName, phoneNumber, currUser);
		try{
			user = userService.addOneUser(user);
			if (StringUtils.isNotBlank(user.getMessage())) {
				super.renderJsonExcuteFail(user.getMessage(), response);
			}else {
				super.renderJsonExcuteSuccess(response);			
			}
		}catch(Exception ex){
			super.renderException(ex, response);
		}		
	}
	
	/**
	 * 删除用户
	 * @param request
	 * @param response
	 */
	@RequestMapping("/deleteOneUser.do")
	public void deleteOneUser(HttpServletRequest request, HttpServletResponse response){
		String id = RequestUtils.getQueryParam(request, "id");
		userService.deleteOneUser(id);
		super.renderJsonExcuteSuccess(response);
	}
	
	/**
	 * 编辑用户
	 * @param request
	 * @param response
	 */
	@RequestMapping("/editOneUser.do")
	public void editOneUser(HttpServletRequest request, HttpServletResponse response){
		String currUser = super.getCurrUser(request).getUserName();
		String id = RequestUtils.getQueryParam(request, "id");
		String realName = RequestUtils.getQueryParam(request, "realName");
		String email = RequestUtils.getQueryParam(request, "email");
		String phoneNumber = RequestUtils.getQueryParam(request, "phoneNumber");
		User user = new User();
		user.setId(id);
		user.setRealName(realName);
		user.setUserEmail(email);
		user.setPhoneNumber(phoneNumber);
		user.setUpdater(currUser);
		user.setUpdateTime(new Date());
		userService.editOneUser(user);
		super.renderJsonExcuteSuccess(response);
	}
	
	/**
	 * 根据Id获取用户对象
	 * @param request
	 * @param response
	 */
	@RequestMapping("/getOneUserById.do")
	public void getOneUserById(HttpServletRequest request, HttpServletResponse response){
		String id = RequestUtils.getQueryParam(request, "id");
		User user = userService.getOneUserById(id);
		super.renderOneObject(user, response);		
	}
	
	/**
	 * 获取用户信息（分页查询）
	 * @param response
	 */
	@RequestMapping("/getUsersInPage.do")
	public void getUsersInPage(HttpServletRequest request, HttpServletResponse response){
		Criteria criteria = super.getPaginationCriteria(request);
		String kssjStr = RequestUtils.getQueryParam(request, "kssj");
		String jssjStr = RequestUtils.getQueryParam(request, "jssj");
		String realNameStr = RequestUtils.getQueryParam(request, "realName");
		String isStopedStr = RequestUtils.getQueryParam(request, "isStoped");
		criteria.setCondition("kssj"+ FrameConstants.NORMAL_FLAG + kssjStr, "jssj"+ FrameConstants.NORMAL_FLAG + jssjStr,
				"realName"+ FrameConstants.NORMAL_FLAG + realNameStr,
				FrameConstants.COMBBOX_CONDITION_PREFIX+"isStoped"+ FrameConstants.NORMAL_FLAG + isStopedStr);
		Pagination dataPage = userService.getUsersInPage(criteria);
		super.renderDatePagination(dataPage, response);
	}
	
}
